package net.introlab.scraper.stockcharts;

import com.gargoylesoftware.htmlunit.html.HtmlPage;
import net.introlab.scraper.domain.Article;
import net.introlab.scraper.domain.filter.ArticleFilter;
import net.introlab.scraper.scanner.BaseJsoupScanner;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.concurrent.Callable;

public class StockChartsScanner extends BaseJsoupScanner<Article, ArticleFilter> {

    @Override
    public String getSiteURL() {
        return "http://stockcharts.com/search/";
    }

    @Override
    protected String applyFilter(String siteURL, ArticleFilter filter) {
        String searchText = filter.getSearchText().replaceAll("\\s+", "%20");
        return siteURL + "?q=" + searchText;
    }

    @Override
    protected Document getPage(int pageNumber, String baseURI) {
        String delimiter = getParametersDelimiter(baseURI);
//        http://stockcharts.com/search/?q=Microsoft&section=blogs&start=0
        pageNumber = pageNumber * 40 - 40;
        String url = String.format("%s%sstart=%s", baseURI, delimiter, pageNumber);
        HtmlPage htmlPage = getHtmlPage(url);
        return parseHtml(htmlPage.asXml());
    }

    @Override
    protected boolean processNextPage(Document doc) {
        return true;
    }

    @Override
    protected boolean isProcessCurrentPage(Document doc) {
        Elements elements = doc.select("#search-results strong:first-of-type");
        return elements.isEmpty();
    }

    @Override
    protected Callable<Article> getDataScraper(Element element) {
        return new StockChartsScraper(element);
    }

    @Override
    protected Elements getElements(Document doc) {
        return doc.select(".section-blogs .search-title a");
    }
}
